<template>
    <div class="login-container">
        <div class="login-box">
            <h1>后台管理系统</h1>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item :label="'密\xa0\xa0\xa0码'" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="captcha">
                    <div class="captcha-box">
                        <el-input v-model="ruleForm.captcha" @keydown.enter.native="submitForm('ruleForm')"></el-input>
                        <img :src="ruleForm.captchaImg" alt="验证码" @click="getCaptcha">
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button class="login-btn" type="primary" @click="submitForm('ruleForm')">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>

import { validateUserName } from '@/utils/validate'
import { getCaptchaApi, loginApi } from '@/request/api'

export default {
    name: 'Login',
    data() {
        return {
            ruleForm: {
                username: '',
                password: '',
                captcha: '',
                captchaImg: ''
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { validator: validateUserName, trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur', }
                ],
                captcha: [
                    { required: true, message: '请输入验证码', trigger: 'blur', }
                ]
            }
        }
    },
    created() {
        this.getCaptcha()
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    let res = await loginApi({
                        username: this.ruleForm.username,
                        password: this.ruleForm.password,
                        captcha: this.ruleForm.captcha
                    })
                    if (res.code === 200) {
                        localStorage.setItem('auth-token',res.data)
                        this.$message.success('登录成功！')
                        setTimeout(() => {
                            this.$router.replace("/")
                        }, 2000);
                    } else {
                        this.$message.error(res.msg)
                        this.getCaptcha()
                    }
                } else {
                    this.$message({
                        showClose: true,
                        message: '请输入正确的用户信息',
                        type: 'error'
                    })
                    return false;
                }
            });
        },
        async getCaptcha() {
            let res = await getCaptchaApi()
            if (res.code === 200) {
                this.ruleForm.captchaImg = res.data
            } else {
                this.$message.error(res.msg)
            }

        }
    },
}
</script>

<style lang="less" scoped>
.login-container {
    width: 100%;
    height: 100%;
    background-image: url(../../assets/login-bg.jpg);
    background-size: cover;
    position: relative;

    .login-box {
        width: 400px;
        background-color: white;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        padding-top: 20px;
        padding-right: 40px;
        border-radius: 10px;

        h1 {
            text-align: center;
            margin-bottom: 20px;
            padding-left: 40px;
            font-size: 20px;
        }

        .captcha-box {
            display: flex;

            img {
                background-color: skyblue;
                height: 40px;
                margin-left: 20px;
                cursor: pointer;
            }
        }

        .login-btn {
            width: 100%;
            margin-left: -40px;
        }
    }
}
</style>